<?php
/**
 * Created by PhpStorm.
 * User: Coderjianfeng
 * Date: 15/8/17
 * Time: 下午3:21
 */
//code 0成功 1失败
class Api_User extends PhalApi_Api{

    public function getRules(){
        return array(
            'login' => array(
                'phoneStr' => array('name' => 'phone', 'type' => 'string', 'require' => true),
                'password' => array('name' => 'password', 'type' => 'string', 'require' => true),
            ),
            'register' => array(
                'phone' => array('name' => 'phone', 'type' => 'string', 'require' => true),
                'password' => array('name' => 'password', 'type' => 'string', 'require' => true),
            ),
            'resetPwd' => array(
                'id' => array('name' => 'id', 'type' => 'int', 'require' => true),
                'password' => array('name' => 'password', 'type' => 'string', 'require' => true),
            ),
        );
    }
    //DI()->logger->debug('user not found',$this->phoneStr,$this->password);
    /**
     * 登录接口
     * @return int code 返回码 0成功 1不存在用户 2密码错误
     * @return string msg 返回信息
     */
    public function login(){
        $rs = array('code' => 0, 'msg' => 'success');

        $domain = new Domain_User();
        $info = $domain->getUser($this->phoneStr,$this->password);
        if(empty($info)){
            $rs['code'] = 2;
            $rs['msg'] = T('password is not current');
            return $rs;
        }
        if(isset($info['code'])){
            $rs['code'] = 1;
            $rs['msg'] = T('user not found');
            return $rs;
        }
        //$rs['info'] = $info;
        return $rs;
    }

    /**
     * 注册接口
     * @return int code 返回码 0成功 1失败
     * @return string msg 返回信息
     */
    public function register(){
        $rs = array('code' => 0, 'msg' =>'success');
        $domain = new Domain_User();
        $info = $domain->register($this->phone,$this->password);
        if($info<=0){
            $rs['code'] = 1;
            $rs['msg'] = T('insert failed');
            return $rs;
        }
        return $rs;
    }

    /**
     * 密码修改
     * @return int code 返回码 0成功 1失败
     * @return string msg 返回信息
     */
    public function resetPwd(){
        $rs = array('code' => 0, 'msg' =>'success');
        $domain = new Domain_User();
        $info = $domain->resetPwd($this->id, $this->password);
        if(!$info){
            $rs['code'] = 1;
            $rs['msg'] = T('update failed');
            return $rs;
        }
        return $rs;
    }


}